layui.use(['form','jquery','layer','table','laydate'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    //更新人赋值
    var user = JSON.parse(localStorage.getItem("user"));
    $('#updateId').val(user.data.userId)
    //让方法在部门id赋值之后执行
    setTimeout(getAuthoritysByDeptId,100);
    //关闭按钮
    $('#close').click(function () {
        var layer = parent.layer;
        layer.close(layer.index);
    });
    //更改按钮
    $('#updateDept').click(function () {
        var deptId = $('#deptId').val();
        //更改部门信息
        updateDeptMessage(form.val('deptForm'));
        //删除部门所拥有的权限
        delDeptAuthorityByDeptId(deptId);

        var layer = parent.layer;
        layer.close(layer.index);
    })
    //根据id查询部门所有权限
    function getAuthoritysByDeptId(){
        var deptId = $('#deptId').val();
        $.ajax({
            url: '/JsDeptAuthority/getAuthoritysByDeptId',
            data: {'deptId': deptId},
            dataType: 'json',
            success: function (data) {
                var unitType = [];
                var trId = data.data;
                //给复选框赋值
                for (var i = 0; i < trId.length; i++) {
                    switch (trId[i].trId) {
                        case 1 :
                            unitType.push('管理员')
                            break;
                        case 2 :
                            unitType.push('教师')
                            break;
                        case 3 :
                            unitType.push('运营')
                            break;
                        case 4 :
                            unitType.push('人事')
                            break;
                    }
                }
                for (var j = 0; j < unitType.length; j++) {
                    var unitTypeCheckbox = $("input[name='tr']");
                    for (var i = 0; i < unitTypeCheckbox.length; i++) {
                        if (unitTypeCheckbox[i].title == unitType[j]) {
                            unitTypeCheckbox[i].value = unitType[j];
                            unitTypeCheckbox[i].checked = true;
                        }
                    }
                }
                form.render();
            }
        });
    }
    //更改部门信息
    function updateDeptMessage(param){
        $.ajax({
            url: '/SyDept/updateDeptMessage',
            data: param,
            type: 'post',
            dataType: 'json',
            success:function (data) {

            }
        });
    }
    //删除部门所拥有的权限
    function delDeptAuthorityByDeptId(deptId) {
        $.ajax({
            url: '/JsDeptAuthority/delDeptAuthorityByDeptId',
            data: {'deptId': deptId},
            dataType: 'json',
            success:function (data) {
                //添加更改后部门的权限
                addDeptAuthority(deptId);
            }
        });
    }
    //添加更改后部门的权限
    function addDeptAuthority(deptId) {
        var checkboxs = $('#checkboxBox input');
        var checkboxValue = [];
        for (var i = 0; i < checkboxs.length; i++) {
            if(checkboxs[i].checked){
                checkboxValue.push(checkboxs[i].value);
            }
        }
        var param = {
            trIds: checkboxValue,
            deptId: deptId,
        }
        $.ajax({
            url: '/JsDeptAuthority/addJsDeptAuthority',
            data: param,
            dataType: 'json',
            type: 'post',
            success: function (data) {

            }
        });
    }
})